﻿$(document).on('pageshow', '#cet', function(){
	//display history
	var hisDataString = getCookie('wechat-express-his');
	var hisData = hisDataString ? JSON.parse(unescape(hisDataString)) : Array();
	for(var i in hisData){
		$('#expressHis').append('<li><a href="http://m.kuaidi100.com/index_all.html?type='+hisData[i].expressCompany+"&postid="+hisData[i].expressNum+'&callbackurl=http://wechat.withelper.com/wechat-express/">'+hisData[i].expressNum+'</a></li>');
	}
	$("#expressHis").listview( "refresh" );
	
	//reset input data
	$('#expressNum').val('');
	$('#none').attr('selected', true);
	
	//auto-complete express company
	$('#expressNum').on('input', function(){
		var expressNum = $('#expressNum').val();
		var expressCompany = testExpressCompany(expressNum);
		if(expressCompany){
			$('#expressCompany').val(expressCompany);
		}else{
			$('#expressCompany').val('');
			$('#none').attr('selected', true);
		}
		$('#expressCompany').selectmenu("refresh");
	});
	
	//submit
	$('#submitBtn').on('click', function(){
		var expressCompany = $('#expressCompany').val();
		var expressNum = $('#expressNum').val();
		var newData = {};
		newData.expressCompany = expressCompany;
		newData.expressNum = expressNum;
		hisData.unshift(newData);
		var cookieData = JSON.stringify(hisData.slice(0,9));
		setCookie('wechat-express-his', cookieData);
		window.location.href = "http://m.kuaidi100.com/index_all.html?type="+expressCompany+"&postid="+expressNum+"&callbackurl=http://wechat.withelper.com/wechat-express/";
	});
});